import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

public class CurrencyConverter {
	static List<Forex> forexList = new ArrayList<>();


	public static void load() {
		ObjectInputStream in;

		try {
			in = new ObjectInputStream(
					new FileInputStream("forex.bin"));

			for (int i=0; i<32; i++) {
				Forex f = (Forex)in.readObject();

				// store f in a data structure
				forexList.add( f );

			}
			in.close();

		} catch(Exception e) {
			e.printStackTrace();
		}

	}
	
	public static String convert(double amountFrom, String from, String to) throws Exception{
		load(); // FILE --> Data Structure

		double amountTo; // ??? JPY
		
		Forex  forexFrom = null; // PHILIPPINES
		Forex  forexTo = null;  // JAPAN
		// search FOREX in Data Structure
		for (Forex f: forexList) {
			if (f.country.equals(from)) {
				forexFrom = f;

			}
			
			if (f.country.equals(to)) {
				forexTo   = f;
			}
		}

		// convert 100 PHP to JPY
		amountTo = amountFrom * forexFrom.forexEUR / forexTo.forexEUR;

		// output
		return forexTo.symbol + " " + amountTo;
	}
	
	public static void main(String[] args) throws Exception{
		load(); // FILE --> Data Structure

		double amountFrom = 100; // 100 PHP
		Forex  forexFrom = null; // PHILIPPINES

		double amountTo; // ??? JPY
		Forex  forexTo = null;  // JAPAN

		// search FOREX in Data Structure
		for (Forex f: forexList) {

			if (f.country.equals("PHILIPPINES")) {
				forexFrom = f;

			} else if (f.country.equals("JAPAN")) {
				forexTo   = f;
			}
		}

		// convert 100 PHP to JPY
		amountTo = amountFrom / forexTo.forexPHP;

		// output
		System.out.println(amountFrom + " " + forexFrom.symbol);
		System.out.println("is equal to");
		System.out.println(amountTo + " " + forexTo.symbol);
	}
}